/**
 * 
 */
package remato.client.model;

import remato.client.controller.RequirementPlugin;
import remato.common.domain.helpers.Nameable;

import com.wgo.bpot.common.removeme.Persistent;

/**
 * @author petterei
 *
 * @version $Id: NameableWrapper.java,v 1.2 2006-02-06 09:29:08 petterei Exp $
 */
public abstract class NameableWrapper extends ClientModelItem {
	private String textCache = "";
	private String simpleNameCache = "";
	private String toolTipTextCache = "";

	static {
    	PropertySpecification<Persistent> titlePropertySpesification = new PropertySpecification<Persistent>(Configuration.getInstance().getNextBaseDefinitionId(), "Title", PropertyType.STRING) {
            @Override
            protected void setValue(Persistent persistent, Object value) {
                String stringValue = getPropertyType().createStringValue(value);
                ((Nameable)persistent).setName(stringValue);
            }
			@Override
			protected Object getValue(Persistent persistent) {
				return ((Nameable)persistent).getName();
			}
        };
        titlePropertySpesification.setDescription("The title of the element.");
        titlePropertySpesification.setPosition(Configuration.getInstance().getNextBaseDefinitionPosition());
        titlePropertySpesification.setDefaultValue("No title set.");
        Configuration.getInstance().addNodeSpesification(titlePropertySpesification);
        Configuration.getInstance().setNodeTitleSpesification(titlePropertySpesification);
        
        PropertySpecification<Persistent> descriptionPropertySpesification = new PropertySpecification<Persistent>(Configuration.getInstance().getNextBaseDefinitionId(), "Description", PropertyType.TEXT) {
            @Override
            public void setValue(Persistent persistent, Object value) {
                String stringValue = getPropertyType().createStringValue(value);
                ((Nameable)persistent).setDescription(stringValue);
            }
			@Override
			public Object getValue(Persistent persistent) {
				return ((Nameable)persistent).getDescription();
			}
        };
        descriptionPropertySpesification.setDescription("The description of the element.");
        descriptionPropertySpesification.setPosition(Configuration.getInstance().getNextBaseDefinitionPosition());
        descriptionPropertySpesification.setDefaultValue("No description set.");
        Configuration.getInstance().addNodeSpesification(descriptionPropertySpesification);
        Configuration.getInstance().setNodeDescriptionSpecification(descriptionPropertySpesification);
    }

	public NameableWrapper() { // requirement system
		super();
	}
    public NameableWrapper(Persistent persistent) {
		super(persistent);
		initPropertiesFromDomainObject(Configuration.getInstance().getNodeSpesifications().values());
	}

    /**
     * @see remato.client.model.facade.ILabelProviderItem#getText()
     */
    public String getText() {
    	if (RequirementPlugin.getInstance().isLoggedIn()) {
	    	String typeName = this.getDomainModelObject().getClass().getInterfaces()[0].getSimpleName();
	        String instanceName = getSimpleName();
	        if (isDirty()) {
	        	typeName = "* " + typeName;
	        }
	        textCache = typeName + ": " + instanceName ;
    	}
    	return textCache;
    }


    public String getSimpleName() {
    	if (RequirementPlugin.getInstance().isLoggedIn()) {
	        AbstractPropertySpesification instanceNameIdentifier = Configuration.getInstance().getNodeTitleSpesification();
	        PropertyValue instanceNamePropertyValue = getCurrentPropertyValue(instanceNameIdentifier);
	        String instanceName = "";
	        if (instanceNamePropertyValue != null) { // Should not occur, for other than invisible root..
	            instanceName = (String)instanceNamePropertyValue.getValue();
	            if (null == instanceName) {
	            	instanceName = instanceNameIdentifier.getDefaultValue();
	            }
	        }
	        simpleNameCache = instanceName ;
    	}
    	return simpleNameCache;
	}

	/**
     * @see remato.client.model.facade.ILabelProviderItem#getToolTipText()
     */
    public String getToolTipText() {
    	if (RequirementPlugin.getInstance().isLoggedIn()) {
	        AbstractPropertySpesification instanceDescriptionIdentifier = Configuration.getInstance().getNodeDescriptionSpecification();
	        PropertyValue instanceDescriptionPropertyValue = getCurrentPropertyValue(instanceDescriptionIdentifier);
	        String instanceDescription = "";
	        if (instanceDescriptionPropertyValue != null) { // Should not occur, for other than invisible root..
	            instanceDescription = (String)instanceDescriptionPropertyValue.getValue();
	            if (null == instanceDescription) {
	            	instanceDescription = instanceDescriptionIdentifier.getDefaultValue();
	            }
	        }
	        toolTipTextCache = instanceDescription + " [dbID: " + getIdentifier() + "]";
    	}
    	return toolTipTextCache;
    }


}
